package cn.pli.bike.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.util.Util;
import com.framemodule.base.BaseActivityPresenter;
import com.framemodule.base.netstatus.NetUtils;
import com.framemodule.utils.CommonUtils;
import com.framemodule.utils.PopupWindowUtil;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.nio.ByteBuffer;

import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import cn.pli.bike.R;
import cn.pli.bike.bean.HttpResult;
import cn.pli.bike.bean.ShareBean;
import cn.pli.bike.bean.UserBean;
import cn.pli.bike.bean.UserInfoSetBean;
import cn.pli.bike.model.UserModel;
import cn.pli.bike.model.subscribers.ProgramSubscriber;
import cn.pli.bike.utils.AppUtils;
import cn.pli.bike.utils.UtilData;

import static cn.pli.bike.ui.UserInfoDelegate.SETTING;
import static cn.pli.bike.ui.UserInfoDelegate.WX_SHARE;
import static cn.pli.bike.ui.UserInfoDelegate.YOUE_COUPON;
import static cn.pli.bike.ui.UserInfoDelegate.YOU_CALL_CENTER;
import static cn.pli.bike.ui.UserInfoDelegate.YOU_CARD;
import static cn.pli.bike.ui.UserInfoDelegate.YOU_JOURNEY;
import static cn.pli.bike.ui.UserInfoDelegate.YOU_SHARE;
import static cn.pli.bike.ui.UserInfoDelegate.YOU_WALLET;

public class UserInfoActivity extends BaseActivityPresenter<UserInfoDelegate> implements View.OnClickListener {
    private UserBean userBean;//UserInfoActivity

    public IWXAPI api;      //这个对象是专门用来向微信发送数据的一个重要接口,使用强引用持有,所有的信息发送都是基于这个对象的

    public PopupWindowUtil popupWindowUtil;

    public void registerWeChat(Context context) {   //向微信注册app
        if (api == null) {
            api = WXAPIFactory.createWXAPI(context, UtilData.APP_ID, true);
        }
        api.registerApp(UtilData.APP_ID);
    }


    @Override
    protected void initModel() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // viewDelegate.setData();
        registerWeChat(this);
        setdata();


    }


    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.tv_user_info_phone, R.id.tv_user_info_credit, R.id.iv_title_back, R.id.iv_user_photo);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //用户电话
            case R.id.tv_user_info_phone:
                break;
            //信用积分
            case R.id.tv_user_info_credit:
                readyGo(CreditRecordActivity.class);
                break;
            case R.id.iv_title_back:
                finish();
                break;
            //点击头像
            case R.id.iv_user_photo:
                Bundle bundle = new Bundle();
                bundle.putSerializable("userBean", userBean);
                readyGo(UserInfoDetailActivity.class, bundle);
                break;
        }


    }

    private void getUserInfo() {
        UserModel.postloginByToken(new ProgramSubscriber<HttpResult<UserBean>>() {
            @Override
            public void onNext(HttpResult<UserBean> userBeanHttpResult) {
                userBean = userBeanHttpResult.getData();
                AppUtils.saveUserInfo(userBeanHttpResult.getData());
                viewDelegate.updateUserInfo(userBeanHttpResult.getData());

            }
        });
    }


    /**
     * 设置数据
     */
    private void setdata() {
        viewDelegate.getUserInfoAdapter().setOnRVItemClickListener(new BGAOnRVItemClickListener() {
            @Override
            public void onRVItemClick(ViewGroup parent, View itemView, int position) {
                UserInfoSetBean userInfoSetBean = viewDelegate.getUserInfoAdapter().getData().get(position);
                switch (userInfoSetBean.getType()) {
                    //我的钱包
                    case YOU_WALLET:
                        readyGo(YourWalletActivity.class);
                        break;

                    //我的行程
                    case YOU_JOURNEY:
                        readyGo(JourneyDetailActivity.class);
                        break;
                    //分享
                    case YOU_SHARE:
                        break;
                    //客服中心
                    case YOU_CALL_CENTER:
                        readyGo(CallCenterActivity.class);
                        break;
                    //设置
                    case SETTING:
                        readyGo(SettingActivity.class);
                        break;
                    //我的优惠券
                    case YOUE_COUPON:
                        readyGo(CouponDetailActivity.class);
                        break;
                    //分享
                    case WX_SHARE:
                        getShareData();
                        break;
                }

            }
        });
    }

    private void getShareData() {
        UserModel.getShareAttribute(new ProgramSubscriber<HttpResult<ShareBean>>() {
            @Override
            public void onNext(HttpResult<ShareBean> shareBeanHttpResult) {
                sharein(shareBeanHttpResult);

            }
        });
    }

    /**
     * 分享内容
     *
     * @param shareBeanHttpResult 分享内容
     * @param scene               分享平台
     */

    private void startShare(HttpResult<ShareBean> shareBeanHttpResult, int scene) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = shareBeanHttpResult.getData().getShareLink();
        WXMediaMessage msg = new WXMediaMessage(webpage);
        if (CommonUtils.isNotEmpty(shareBeanHttpResult.getData().getShareTitle())) {
            msg.title = shareBeanHttpResult.getData().getShareTitle();
        } else {
            msg.title = " ";
        }

        if (CommonUtils.isNotEmpty(shareBeanHttpResult.getData().getShareContent())) {
            msg.description = shareBeanHttpResult.getData().getShareContent();
        } else {
            msg.description = " ";
        }
        Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.kh_logo);
        msg.thumbData = cn.pli.bike.wxapi.Util.bmpToByteArray(thumb, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();    //创建一个请求对象
        req.message = msg;  //把msg放入请求对象中
        // req.scene = SendMessageToWX.Req.WXSceneTimeline;    //设置发送到朋友圈
        // req.scene = SendMessageToWX.Req.WXSceneSession;   //设置发送给朋友
        req.scene = scene;
        req.transaction = buildTransaction("web");
        boolean b = api.sendReq(req);   //如果调用成功微信,会返回true
    }


    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    private void sharein(final HttpResult<ShareBean> shareBeanHttpResult) {
        popupWindowUtil = new PopupWindowUtil(viewDelegate.getRootView());// R.layout.pop_magazine_share_new
        View contentView = View.inflate(this, R.layout.pop_moment_share_new, null);
        popupWindowUtil.setContentView(contentView);
        popupWindowUtil.showBottomWithAlpha();
        contentView.findViewById(R.id.share_pengyouquan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startShare(shareBeanHttpResult, SendMessageToWX.Req.WXSceneTimeline);
                popupWindowUtil.dismiss();
            }
        });
        contentView.findViewById(R.id.share_weixinfenxiang).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startShare(shareBeanHttpResult, SendMessageToWX.Req.WXSceneSession);
                popupWindowUtil.dismiss();
            }
        });
        contentView.findViewById(R.id.ppw_share_cancal_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindowUtil.dismiss();
            }
        });
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindowUtil.dismiss();
            }
        });

    }


}
